package com.longrise.android.byjk.plugins.aboutme.personalInfo;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.plugins.main.UpdataAPK;
import com.longrise.android.byjk.common.CloseModeConstants;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.plugins.aboutme.SettingActivity;
import com.longrise.android.byjk.plugins.login.LoginActivity;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.ActivityUtil;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.DZZWTools;
import com.longrise.android.byjk.utils.DialogUtil;
import com.longrise.android.byjk.utils.Tracker_Google;
import com.longrise.filedownloader.DownloadManager;
import com.longrise.filedownloader.DownloadService;

/**
 * Created by godliness on 2017/3/7.
 *
 */

public class SettingPresenter implements PersonalInfoContract.SettingPresenters {

    private static final String TAG = "SettingPresenter";

    private PersonalInfoContract.SettingView mSetView;
    private Context mCxt;

    public SettingPresenter(PersonalInfoContract.SettingView view, SettingActivity act) {
        this.mSetView = view;
        this.mCxt = act;
    }

    @Override
    public void init() {
        mSetView.setTitle("设置");
        firstCheckUpdate();

    }

    @Override
    public void jumpPersion() {

    }

    @Override
    public void jumpZH() {

    }

    @Override
    public void jumpCache() {

    }

    @Override
    public void jumpSharebb() {

    }

    @Override
    public void jumpCheckUpdate() {
        checkUpdate();
    }

    @Override
    public void jumpSuggest() {

    }

    @Override
    public void jupmAboutbb() {

    }

    @Override
    public void firstCheckUpdate() {
        String appname = AppUtil.getString(R.string.leapAppName);
        EntityBean bean = new EntityBean();
        bean.set("appname", appname);
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl, "bbt_getAppVersion",
                fbean, new LoadDataManager.OnRequestCallListener() {
            @Override
            public void onSuccess(String key, String service, Object result) {
                EntityBean _checkVersion_eb = (EntityBean) result;
                Integer resultstate = _checkVersion_eb.getInt("resultstate");
                if(resultstate==1){
                    EntityBean resultbean = _checkVersion_eb.getBean("result");
                    String description = resultbean.getString("description");
                    mSetView.setVersionCode(description);
                }
            }

            @Override
            public void onFinished(String key, String service) {

            }

            @Override
            public void onError(String key, String service, Throwable ex, boolean isOnCallback) {

            }
        });

    }

    @Override
    public void exit() {
        exitSystemHint();
    }

    @Override
    public void colse() {
        mSetView.close();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

    }

    private void checkUpdate() {
        UpdataAPK mUpdataAPK = new UpdataAPK(mCxt, AppUtil.getString(R.string.leapAppName),true);
        mUpdataAPK.setCheckVersionListener(new UpdataAPK.CheckVersionListener() {
            @Override
            public void onCheckVersion(int mark) {
                if (mark == 0 ) { //app没有更新
                    DZZWTools.showToast(mCxt, "当前已是最新版本", Toast.LENGTH_SHORT);
                }
            }
        });
        mUpdataAPK.checkVersion();
    }

    private void exitSystemHint() {
        View v = View.inflate(mCxt, R.layout.dialog_exit, null);
        final TextView text_Cacnle = (TextView) v.findViewById(R.id.text_cancle);
        final TextView text_Confrim = (TextView) v.findViewById(R.id.text_confirm);
        View.OnClickListener listener = new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                if (view.getId() == R.id.text_confirm) {
                    try{
                        DownloadManager downloadManager = DownloadService.getDownloadManager();
                        if(downloadManager != null){
                            downloadManager.onDestory();
                        }
                        CloseModeConstants.setWillToLogin(true);
                        com.longrise.common.datasource.remote.LoadDataManager.getInstance().setReloginBean(null);

                        mSetView.removeFromActivityUtils();
                        ActivityUtil.finishAllAcivity();
                        Intent intent = new Intent(mCxt, LoginActivity.class);
                        mCxt.startActivity(intent);

//                        new BbJpushHelper().stopAliasNotification(mCxt);



                        mSetView.close();
                        Tracker_Google.getInstance().addEvent("Exit-button", "Exit-byself", "手动退出账号");
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
                DialogUtil.getInstance().dismiss();
            }
        };
        text_Cacnle.setOnClickListener(listener);
        text_Confrim.setOnClickListener(listener);
        DialogUtil.getInstance().creatAlertDialog(mCxt, v, 266, 159);
    }
}
